<?php
require_once '../include.php';

use sdk\Image;
use sdk\WeiXin;
use sdk\Db;

################################################################################
// 商店id
$shop_id = 1;
// iBeacon参数
$device_id = '891455';
$uuid = 'FDA50693-A4E2-4FB1-AFCF-C6EB07647825'; 
$major= '10010'; 
$minor= '23412';
################################################################################


try {
    
    // 用户服务
    $cwx = new WeiXin($customerAppName, $customerAppId, $customerAppSecret);
    // 商户服务
    $swx = new WeiXin($serverAppName, $serverAppId, $serverAppSecret);
    // 数据库服务
    $db = new Db($host, $username, $passwd, $schema);

    // 初始化商店
    $db->initShop($shop_id);

    // 初始化iBeacon设备
    initIBeacon($db, $shop_id, 0, $device_id, $uuid, $major, $minor);
    
    // 初始化用户二维码
    initQRcode($cwx, $db, $shop_id, 2);

    // 初始化店主二维码
    initQRcode($swx, $db, $shop_id, 0);

    // 初始化店员二维码
    initQRcode($swx, $db, $shop_id, 1);

} catch (Exception $e) {
    var_dump($e->getMessage());
}

function initQRcode($wx, $db, $shop_id, $role) {
    // 嵌入二维码中的场景id
    $scene_id = $shop_id . $role;

    $json_str = '{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": '
            . $scene_id . '}}}';

    // 生成二维码
    $url = $wx->createQRcodeUrl($json_str);

    // 将二维码以文件形式保存在服务器端
    $file_name = uniqid() . '.jpg';
    $image_full_name = '/var/tmp/' . $file_name;
    exec('curl -G "' . $url . '" > ' . $image_full_name);

    // 将二维码文件上传到万象图片
    $uploadRet = Image::upload($image_full_name, $scene_id);

    // 删除服务器端文件
    unlink($image_full_name);

    if (0 === $uploadRet['code']) {
        $fileid = $uploadRet['data']['fileid'];
        $param = array($fileid, null, null, null);
        $db->createDevice($shop_id, $role, 0, $param);
    } else {
        var_dump($uploadRet);
    }
}

function initIBeacon($db, $shop_id, $role, $device_id, $uuid, $major, $minor) {
    $param = array($device_id, $uuid, $major, $minor);
    $db->createDevice($shop_id, $role, 1, $param);
}

//    // 查询管理信息
//    $statRet = Image::stat($fileid);
//    var_dump($statRet);
//
//    // 复制
//    $copyRet = Image::copy($fileid);
//    var_dump($copyRet);
//
//    // 生成私密下载url
//    $downloadUrl = $copyRet['data']['downloadUrl'];
//    $sign = Auth::appSign($downloadUrl, 0);
//    $signedUrl = $downloadUrl . '?sign=' . $sign;
//    var_dump($signedUrl);
//
//    //生成新的上传签名
//    $expired = time() + 999;
//    $sign = Auth::appSign('http://web.image.myqcloud.com/photos/v1/200679/0/', $expired);
//    var_dump($sign);
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
<?php
?>
    </body>
</html> 


